package com.service;
import static com.def.GenericDef.ACTION_FLG_ADD;
import static com.def.GenericDef.ACTION_FLG_DELETE;
import static com.def.GenericDef.ACTION_FLG_QUERY;
import static com.def.GenericDef.ACTION_FLG_UPDATE;
import static com.def.GenericDef.RETURN_CODE_OK;
import static com.def.GenericDef.RETURN_CODE_UNKNOWN;

import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.dao.IBisMtrlDao;
import com.icim.service.ICIMBaseService;
import com.tx.Xpbismtr.XpbismtrI;
import com.tx.Xpbismtr.XpbismtrO;
import com.util.MqOpe;

@Scope("prototype")
@Service("XpbismtrService")
public class XpbismtrService extends ICIMBaseService{
	private Logger logger = Logger.getLogger(XpbismtrService.class);
	@Autowired
	private XpbismtrI xpbismtrI;
	
	@Autowired
	private XpbismtrO xpbismtrO; 
	
	
	@Autowired
	private IBisMtrlDao bisMtrlDao;
	
	@Autowired
	private MqOpe mqOpe;
	public String subMainProc(String strInMsg){
		logger.info("InTrx is "+ strInMsg );
		Session session = bisMtrlDao.getCrSession();
		try{
			xpbismtrI = (XpbismtrI) mqOpe.inStr2Obj(strInMsg,xpbismtrI);
			char action_flg = xpbismtrI.getAction_flg().charAt(0);
			switch( action_flg ){
				case ACTION_FLG_QUERY   : xpbismtrO = queryByCondition()  ; break;   
				case ACTION_FLG_ADD     : xpbismtrO = addFnc()            ; break;
				case ACTION_FLG_UPDATE  : xpbismtrO = updateFnc()         ; break;
				case ACTION_FLG_DELETE  : xpbismtrO = deleteFnc()         ; break;
				case 'G'                : xpbismtrO = queryformkr()       ; break;
				case 'F'                : xpbismtrO = queryfordsc()       ; break;
				default                 : xpbismtrO = errActionFnc();   
			}
			if( RETURN_CODE_OK.equals(xpbismtrO.getRtn_code()) ){
				session.flush();
			}else{
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			}
		}catch(Exception ex){
			xpbismtrO.setTrx_id("XPBISMTR");
			xpbismtrO.setType_id("O");
			xpbismtrO.setRtn_code(RETURN_CODE_UNKNOWN);
			xpbismtrO.setRtn_mesg(ex.getCause().toString());
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			//TODO:throw dbRollbackException
		}finally{
			logger.info(mqOpe.obj2Str(xpbismtrO));
		}
		return mqOpe.obj2Str(xpbismtrO);
	}
	public XpbismtrO addFnc() throws Exception {
		XpbismtrO  XpbismtrO = bisMtrlDao.addProc(xpbismtrI);
		return XpbismtrO;
	}
	public XpbismtrO updateFnc(){
		XpbismtrO  xpbmdldfO = bisMtrlDao.updateProc(xpbismtrI);
		return xpbmdldfO;
	}
	public XpbismtrO deleteFnc(){
		XpbismtrO  xpbmdldfO = bisMtrlDao.deleteProc(xpbismtrI);
		return xpbmdldfO;
	}
	public XpbismtrO queryByCondition(){
		XpbismtrO  xpbmdldfO = bisMtrlDao.queryByCondition(xpbismtrI);
		return xpbmdldfO;
	}
	public XpbismtrO queryformkr(){
		XpbismtrO  xpbmdldfO = bisMtrlDao.queryFormkr(xpbismtrI);
		return xpbmdldfO;
	}
	public XpbismtrO queryfordsc(){
		XpbismtrO  xpbmdldfO = bisMtrlDao.queryFordsc(xpbismtrI);
		return xpbmdldfO;
	}
	public XpbismtrO errActionFnc(){
		XpbismtrO  xpbmdldfO = new XpbismtrO();
		xpbmdldfO.setTrx_id("XPBISMTR");
		xpbmdldfO.setType_id("O");
		xpbmdldfO.setRtn_code("0000009");
		xpbmdldfO.setRtn_mesg("Invalid Action_flg");
		return xpbmdldfO;
	}
	
}
